package com.example.myapplication.activity.listView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.myapplication.R;

import java.util.List;

/**
 * Create By TreeRoot
 * Create Time: 2023/12/27 02:35
 * Class Name: CustomAdapter
 * Description:
 *
 * @author TreeRoot
 */
public class CustomAdapter extends ArrayAdapter<MyOption> {
    private Context mContext;
    private int mResource;

    public CustomAdapter(Context context, int resource, List<MyOption> objects) {
        super(context, resource, objects);
        mContext = context;
        mResource = resource;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(mResource, parent, false);

            holder = new ViewHolder();
            holder.textView = convertView.findViewById(R.id.text);
            holder.imageView = convertView.findViewById(R.id.icon);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        MyOption option = getItem(position);

        if (option != null) {
            holder.textView.setText(option.getText());
            holder.imageView.setImageResource(option.getIcon());
        }

        return convertView;
    }

    static class ViewHolder {
        TextView textView;
        ImageView imageView;
    }
}